package algorithm2_v3;

import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Map;

class Message implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	StringBuilder type;
	StringBuilder sender;
	Map<String, Integer> recovered;

	Message(String type) throws UnknownHostException {
		this.sender = new StringBuilder(InetAddress.getLocalHost()
				.getHostName());
		this.type = new StringBuilder(type);
		if (type.equals("LEADER"))
			this.recovered = SystemInfo.INSTANCE.recovered;
	}

	public void sendMessages() {
		SystemInfo info = SystemInfo.INSTANCE;

				// prozesu izenak hartu timeoutetik nere burua ezbaitako bertako
				// keyetan
				for (String host : info.timeout.keySet()) {
					try {
						Socket socket = new Socket(host, 12345);
						ObjectOutputStream clientOutputStream = new ObjectOutputStream(
								socket.getOutputStream());
						clientOutputStream.writeObject(this);
						clientOutputStream.close();
						socket.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			


	}
}
